******************************************************************************************
* Do-file name:	an_dec_employ_01.do                                                      
* Task:         run regressions on employment decomposition, municipality level          
* Last change:  28.07.2025                                                               
* Notes: 
/*
This file contains the code used to generate the results presented in the following tables and figures:
- Figure 1, Panel A: 
		-- Regional employment effect: g_emp_nat_s1
		-- Displacement effect: dec_emp_12_s1

- Table 1:
		-- Regional employment effect: g_emp_nat_s1
		-- Displacement effect: dec_emp_1_s1
		-- Crowding out effect: dec_emp_3_s1
		-- Relocation effect: dec_emp_2_s1

- Table 3:
		-- Displacement effect, Panel B: dec_eao_2_s6
		-- Displacement effect, Panel C: Routine: dec_etr_2_s2; Abstract: dec_eta_2_s3

- Figure 2, Panel A: 
		-- Manual/routine tasks: dec_etr_1_s2
		-- Abstract tasks: dec_eta_1_s3

- Table 4:
		-- Regional routine employment effect: dec_etr_1_s2
		-- Displacement effect: dec_etr_2_s2
		-- Crowding out effect: dec_etr_8_s2
		-- Relocation effect: dec_etr_7_s2
		-- Upgrades: dec_etr_5a_s2
		-- Downgrades: dec_etr_6a_s2
*/
******************************************************************************************



******************************************************************************************
*** program setup
******************************************************************************************

version 14.2
clear all
macro drop _all
set linesize 90
set more off
* set trace on
discard
set seed 123456789
*set matsize 2000



******************************************************************************************
*** load working dataset
******************************************************************************************

foreach x of numlist 1/3 6 {
use "data/employ_region_s1.dta", clear



******************************************************************************************
*** set globals
******************************************************************************************

* 1) 
global sample_employ_region_s1 "All workers (natives)"
global weight_s1 "emp_nat_90*weight_matching"
global dcomp_s1  "g_emp_nat  dec_emp_12  dec_emp_1  dec_emp_3  dec_emp_2"

* 2) 
global sample_employ_region_s2 "Manual/routine tasks worker (3d)"
global weight_s2 "emp_nat_task3d21_90*weight_matching"
global dcomp_s2  "dec_etr_1  dec_etr_2  dec_etr_8  dec_etr_7  dec_etr_5a  dec_etr_6a"

* 3) 
global sample_employ_region_s3 "Abstract tasks worker (3d)"
global weight_s3 "emp_nat_task3d22_90*weight_matching"
global dcomp_s3  "dec_eta_1  dec_eta_2"

* 6)
global sample_employ_region_s6 "Age 50 and above"
global weight_s6 "emp_nat_age33_90*weight_matching"
global dcomp_s6  "dec_eao_2"

*** use different czech shocks
foreach year of numlist 1987/1990 1992/1995 {
global czech_shock_`year' "czshare_92_90_predic_13"
	}

global czech_shock_1991   "czshare_91_90_predic_13"



******************************************************************************************
*** estimate employment change
******************************************************************************************

*** run different outcome variables
foreach var of global dcomp_s`x' {

foreach y of numlist 1987/1995 {

*** 1) basic model: full sample
reg `var'  ${czech_shock_`y'}  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}], cluster(ao_kreis_imp)
est store `var'`y'


*** check sample
dis "Sample: ${sample_employ_region_s`x'}"
dis "Outcome Variable: `var'"
dis "Estimation weight: ${weight_s`x'}" 
dis "Year = `y'"
dis "Sample restriction version: 16-65, employed natives (pers_gr: 101 & 102)"
count if e(sample)


********* bootstrap standard errors ***************************************************************
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_92_`bs'.dta", nogenerate
	reg `var'  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	scalar coef`bs' = _b[czshare_92_90_predic_13]
	drop y res xb czshare_92_90_predic_13 wild
}
gen bs = . 
gen coef_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_`var'_`y'
keep if bs != .
save "data/bootstrap/emp_decomp/bs_`var'_`y'.dta", replace
sum				// std. dev. = std. error
restore

***************************************************************************************************

********* bootstrap standard errors 1991 **********************************************************
if `x' == 1 & `y' == 1991 {
preserve
keep if e(sample)
drop czshare_91_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_91_`bs'.dta", nogenerate
	reg `var'  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	scalar coef`bs' = _b[czshare_91_90_predic_13]
	drop y res xb czshare_91_90_predic_13 wild
}
gen bs = . 
gen coef_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_`var'_`y'
keep if bs != .
save "data/bootstrap/emp_decomp\1991_cz_shock/bs_`var'_`y'.dta", replace
sum				// std. dev. = std. error
restore
}
***************************************************************************************************
	}


*** create tables
#delimit ;
global estout_employ "cells(b(star fmt(%9.3f) vacant(-)) se(fmt(%9.3f) par)) 
stats(N N_clust r2_a, labels("Observations" "Clusters" "Adjusted R2") layout(@ @ @) fmt(%9.0fc %9.0fc %9.3f))
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
varlabels(_cons "Constant" czshare_92_90_predic_13 "Share Czechs (92-90, pred.)" czshare_91_90_predic_13 "Share Czechs (91-90, pred.)") label
mlabels("Empl. 87-90" "Empl. 88-90" "Empl. 89-90" "Empl. 91-90" "Empl. 92-90" "Empl. 93-90" "Empl. 94-90" "Empl. 95-90")
prehead(@title) posthead() postfoot(@note) nonumbers collabels(none) style(tab)";
#delimit cr


** Table: Employment
sum obs_1990
local obs_1990 = r(mean)

estout  `var'1987  `var'1988  `var'1989  `var'1991  `var'1992  `var'1993  `var'1994  `var'1995 ///
using "tables\employ/tab_`var'_s`x'.txt", $estout_employ replace ///	
title(Employment Decomposition: `var' (1987-1995): Base, Sample: ${sample_employ_region_s`x'}) ///
note(Notes: Regional approach. * p<0.1, ** p<0.05, *** p<0.01. Obs. 1990: `obs_1990'. ///
Data Source: German Social Security Records, border districts and matched control districts, 1987-1995.)
	}
	}


******************************************************************************************
*** end
******************************************************************************************

exit


*========================================================================================*
Comments:
- unique identifier: vsnr_ano year
